package main.java.com.util;

import java.sql.Connection;
import java.io.InputStream;
import java.sql.DriverManager;
import java.util.Properties;

/**
 * !!! Anti-fake label !!!
 * ClassName: ConnDb
 * Package: main.java.com.util
 * Description:
 *
 * @Author 22软件6班 陈彬
 * @Create 2023/11/1 8:48
 * @Version 1.0
 * 测试连接数据库
 **/
public class ConnDb {
    private static String DRIVER_CLASS;//驱动类
    private static String DB_URL;//数据库接口地址
    private static String DB_USER;//数据库用户名
    private static String DB_PASSWORD;//数据库密码
    //创建数据库连接
    public Connection dataBaseConnectionTest(){
        //创建连接对象
        Connection conn = null;
        try {
            //创建properties属性文件对象
            Properties properties = new Properties();
            //将db.properties文件读取到输入流中
            InputStream is = PropertiesRead.class.getClassLoader().getResourceAsStream("db.properties");
            //将文件流加载到properties对象中
            properties.load(is);
            DRIVER_CLASS = properties.getProperty("DRIVER_CLASS");
            DB_URL = properties.getProperty("DB_URL");
            DB_USER = properties.getProperty("DB_USER");
            DB_PASSWORD = properties.getProperty("DB_PASSWORD");
            //加载数据库驱动
            Class.forName(DRIVER_CLASS);
            //创建连接
            conn = DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
            System.out.println("======数据库连接成功======");
        }catch (Exception e){
            e.printStackTrace();
        }
        return conn;
    }
    //测试
    public static void main(String[] args) {
        ConnDb cd = new ConnDb();
        cd.dataBaseConnectionTest();
    }
}
